package com.hxh.component.business.utils.idscan;

import android.content.Context;
import android.support.v4.util.ArrayMap;

import com.hxh.component.basicore.util.rx.RxUtils;
import com.hxh.component.basicore.util.rx.resetfulhttpstyle.ProgressSubScribe;
import com.megvii.idcardquality.IDCardQualityLicenseManager;
import com.megvii.licensemanager.Manager;

import rx.Observable;
import rx.Subscriber;


/**
 * Created by hxh on 2017/12/4.
 */

public class IDCardUtil {

    public static void initIDCardScannerComponent(final Context context) {
        Observable
                .create(new Observable.OnSubscribe<Boolean>() {
                    @Override
                    public void call(Subscriber<? super Boolean> subscriber) {
                        Manager manager = new Manager(context);
                        IDCardQualityLicenseManager idCardLicenseManager = new IDCardQualityLicenseManager(
                                context);
                        manager.registerLicenseManager(idCardLicenseManager);
                        manager.takeLicenseFromNetwork(Util.getUUIDString(context));
                        if (idCardLicenseManager.checkCachedLicense() > 0) {
                            subscriber.onNext(true);
                        } else {
                            subscriber.onNext(false);
                        }
                        subscriber.onCompleted();
                    }
                })
                .compose(RxUtils.<Boolean>io_main())
                .subscribe(new ProgressSubScribe<Boolean>() {
                    @Override
                    public void _OnError(Throwable throwable) {

                    }

                    @Override
                    public void _OnNet(Boolean aBoolean) {

                    }
                });
    }


    public static String genetorRequestBodyForOCRIdCard(ArrayMap<String, String> stringStringArrayMap)
    {
        StringBuffer sb = new StringBuffer("{\n");

        int maxsize = stringStringArrayMap.size();
        int currindex=0;
        for (String key : stringStringArrayMap.keySet()) {
            ++currindex;
            sb.append(" \"" + key + "\":");
            if (key.equals("sms_code")) {
                sb.append("\""+stringStringArrayMap.get(key)+"\"");
            } else
            {
                sb.append(stringStringArrayMap.get(key));
            }

            if(currindex<maxsize)
            {
                sb.append(",");
            }

        }

        sb.append("}");
        return sb.toString();
    }


}
